package com.liujit.bms.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liujit.bms.admin.domain.model.BlogArticleLabel;
import com.liujit.bms.admin.domain.model.BlogLabel;
import com.liujit.bms.admin.domain.params.BlogLabelParams;
import com.liujit.bms.admin.domain.query.BlogLabelQuery;
import com.liujit.bms.admin.domain.result.BlogLabelResult;
import com.liujit.bms.admin.mapper.BlogLabelMapper;
import com.liujit.bms.admin.service.BlogArticleLabelService;
import com.liujit.bms.admin.service.BlogLabelService;
import com.liujit.common.constants.ColumnConstant;
import com.liujit.common.constants.GlobalConstant;
import com.liujit.common.domain.params.BaseParams;
import com.liujit.common.domain.result.PageBean;
import com.liujit.common.exception.BusinessException;
import com.liujit.common.exception.DataNotFoundException;
import com.liujit.common.utils.DozerConvertor;
import com.liujit.common.utils.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description 博客标签Service实现类
 * @Author liujun
 * @Date 2021-07-03 14:37:26
 */
@Service
public class BlogLabelServiceImpl extends ServiceImpl<BlogLabelMapper, BlogLabel> implements BlogLabelService {

    private BlogArticleLabelService blogArticleLabelService;

    @Autowired
    public void setBlogArticleLabelService(BlogArticleLabelService blogArticleLabelService) {
        this.blogArticleLabelService = blogArticleLabelService;
    }

    @Override
    public PageBean<BlogLabelResult> getPage(BlogLabelQuery query) {
        // 排序
        if (StrUtil.isNotBlank(query.getSortName()) && StrUtil.isNotBlank(query.getSortOrder())) {
            query.setSortName(ColumnConstant.CREATE_TIME);
            query.setSortOrder(SqlKeyword.DESC.getSqlSegment());
        }
        Page<BlogLabel> page = Pagination.page(query);
        LambdaQueryWrapper<BlogLabel> wrapper = new LambdaQueryWrapper();
        if (StrUtil.isNotBlank(query.getName())) {
            wrapper.like(BlogLabel::getName, query.getName());
        }
        if (query.getEnabled() != null) {
            wrapper.eq(BlogLabel::getEnabled, query.getEnabled());
        }
        if (query.getStartTime() != null) {
            wrapper.ge(BlogLabel::getCreateTime, query.getStartTime());
        }
        if (query.getEndTime() != null) {
            wrapper.le(BlogLabel::getCreateTime, query.getEndTime());
        }
        IPage<BlogLabel> iPage = super.page(page, wrapper);
        return PageBean.page(iPage, BlogLabelResult.class);
    }

    @Override
    public Boolean save(BlogLabelParams params) {
        BlogLabel entity = DozerConvertor.convertor(params, BlogLabel.class);
        // 添加
        if (params.getId() == null) {
            entity.setEnabled(GlobalConstant.ENABLED_YES);
            return super.save(entity);
        } else {
            BlogLabel blogLabel = super.getById(params.getId());
            if (blogLabel == null) {
                throw new DataNotFoundException();
            }
            return super.updateById(entity);
        }
    }

    @Override
    public BlogLabelResult get(Long id) {
        BlogLabel blogLabel = super.getById(id);
        if (blogLabel == null) {
            throw new DataNotFoundException();
        }
        return DozerConvertor.convertor(blogLabel, BlogLabelResult.class);
    }

    @Override
    public Boolean del(BaseParams params) {
        BlogLabel blogLabel = super.getById(params.getId());
        if (blogLabel == null) {
            throw new DataNotFoundException();
        }
        LambdaQueryWrapper<BlogArticleLabel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BlogArticleLabel::getLabelId, params.getId());
        Integer count = blogArticleLabelService.count(wrapper);
        if (count > 0) {
            throw new BusinessException("标签下有文章，不能删除");
        }
        return super.removeById(params.getId());
    }

    @Override
    public Boolean enabled(BaseParams params) {
        BlogLabel blogLabel = super.getById(params.getId());
        if (blogLabel == null) {
            throw new DataNotFoundException();
        }
        return super.updateById(new BlogLabel() {{
            setId(params.getId());
            setEnabled(GlobalConstant.ENABLED_YES);
        }});
    }

    @Override
    public Boolean disabled(BaseParams params) {
        BlogLabel blogLabel = super.getById(params.getId());
        if (blogLabel == null) {
            throw new DataNotFoundException();
        }
        LambdaQueryWrapper<BlogArticleLabel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BlogArticleLabel::getLabelId, params.getId());
        Integer count = blogArticleLabelService.count(wrapper);
        if (count > 0) {
            throw new BusinessException("标签下有文章，不能禁用");
        }
        return super.updateById(new BlogLabel() {{
            setId(params.getId());
            setEnabled(GlobalConstant.ENABLED_NO);
        }});
    }

    @Override
    public Integer getCountByName(Long id, String name) {
        LambdaQueryWrapper<BlogLabel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BlogLabel::getName, name);
        if (id != null) {
            wrapper.ne(BlogLabel::getId, id);
        }
        return super.count(wrapper);
    }

    @Override
    public List<BlogLabelResult> getList() {
        LambdaQueryWrapper<BlogLabel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BlogLabel::getEnabled, GlobalConstant.ENABLED_YES);
        List<BlogLabel> list = super.list(wrapper);
        return DozerConvertor.convertor(list, BlogLabelResult.class);
    }
}
